package com.heima.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.heima.Result;
import com.heima.domain.dto.PageDTO;
import com.heima.domain.po.HealthLecture;
import com.heima.domain.query.ResidentHealthLectureQuery;
import com.heima.mapper.HealthLectureMapper;
import com.heima.service.IHealthLectureService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 健康讲座表 服务实现类
 * </p>
 *
 * @author author
 * @since 2025-07-28
 */
@Service
public class HealthLectureServiceImpl extends ServiceImpl<HealthLectureMapper, HealthLecture> implements IHealthLectureService {
    //status为1
    public Result findByPage(ResidentHealthLectureQuery residentHealthLectureQuery){
        String name = residentHealthLectureQuery.getName();
        LocalDateTime beginTime = residentHealthLectureQuery.getBeginTime();
        LocalDateTime endTime = residentHealthLectureQuery.getEndTime();
        Integer pageNum= residentHealthLectureQuery.getPageNum();
        Integer pageSize= residentHealthLectureQuery.getPageSize();
        Page<HealthLecture> p = this.lambdaQuery().like(StringUtils.isNotBlank(name), HealthLecture::getName, name)
                .eq(HealthLecture::getStatus, 1)
                .ge(beginTime != null, HealthLecture::getStartTime, beginTime)
                .le(endTime != null, HealthLecture::getStartTime, endTime)
                .orderByDesc(HealthLecture::getStartTime)
                .page(new Page<>(pageNum, pageSize));
        List<HealthLecture> records = p.getRecords();
        if (records.isEmpty()) { // 如果结果为空
            return Result
                    .success(new PageDTO<>(p.getTotal(), p.getSize(), p.getPages(), (long)pageNum, new ArrayList<>())); // 返回空数据的分页结果
        }
        return Result
                .success(new PageDTO<>(p.getTotal(), p.getSize(), p.getPages(), (long)pageSize, records)); // 返回查询到的数据分页结果
    }
}
